/*
 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include "cmsis_os2.h"
#include "ohos_run.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"

#define SYS_DELAY_TICKS 200
#define TASK_STACK_SIZE 4096
#define TASK_PRIO 25

#define ADC1_CHANNEL ADC_CHANNEL_0    // 使用 ADC1 通道 0
#define ADC_ATTEN ADC_ATTEN_DB_11     // 衰减值
#define ADC_BITWIDTH ADC_WIDTH_BIT_12 // 位宽

void mq2_test(void)
{
  adc1_config_width(ADC_BITWIDTH);
  adc1_config_channel_atten(ADC1_CHANNEL, ADC_ATTEN);
  while (1)
  {
    uint16_t adc_val = adc1_get_raw(ADC1_CHANNEL);
    printf("%d\r\n", adc_val);
    sleep(1);
  }
}

static void adc_example_task(void)
{
  osThreadAttr_t attr;
  int g_taskID;

  attr.name = "mq2_test";
  attr.attr_bits = 0U;
  attr.cb_mem = NULL;
  attr.cb_size = 0U;
  attr.stack_mem = NULL;
  attr.stack_size = TASK_STACK_SIZE;
  attr.priority = TASK_PRIO;
  g_taskID = osThreadNew((osThreadFunc_t)mq2_test, NULL, &attr);
  if (g_taskID == NULL)
  {
    printf("Failed to create Test GPIO thread!\n");
  }
}

OHOS_APP_RUN(adc_example_task);
